 //
//  Remote_controlViewController.m
//  CleanerAir_Project
//
//  Created by mac on 16/4/28.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "Remote_controlViewController.h"
#import "GKHScanQCodeViewController.h"
#import "RemoteTableViewCell.h"
#import "CAAddingPurifierVC.h"
#import "CAPurifierDetailsVC.h"
#import "MMWMiotLinkWebService.h"
#import "EquipmentModel.h"
#import "MJRefresh.h"

//RemoteCell

@interface Remote_controlViewController ()<QRScanViewDelegate,UITableViewDelegate,UITableViewDataSource>{


}

@property (weak, nonatomic) IBOutlet UITableView *remoteListTableView;

@property (nonatomic, strong) NSMutableArray *equipmentModelAry;


@end

@implementation Remote_controlViewController

- (NSMutableArray *)equipmentModelAry{
    if (_equipmentModelAry == nil) {
        _equipmentModelAry = [NSMutableArray new];
    }
    return _equipmentModelAry;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text=@"遥控器";
    
    if (![[LoginSingleton sharedLoginSingleton].token isEqualToString:@""] && ![[LoginSingleton sharedLoginSingleton].cuid isEqualToString:@""]) {
        [self getEquipmentListInfoWithMiotLink];
    }

    //导航左btn
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 15, 22, 22)];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"Remote_add"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(scan) forControlEvents:UIControlEventTouchUpInside];
    //    [leftButton setBackgroundImage:[UIImage imageNamed:@"navbtn.png"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    //导航右btn
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 15, 22, 22)];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"扫一扫"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(scan) forControlEvents:UIControlEventTouchUpInside];
    //    [leftButton setBackgroundImage:[UIImage imageNamed:@"navbtn.png"] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
    self.remoteListTableView.dataSource = self;
    self.remoteListTableView.delegate = self;
    
    self.remoteListTableView.mj_header =  [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(hospitalTVheaderRereshing)];
    
    self.remoteListTableView.tableFooterView = [[UIView alloc] initWithFrame:(CGRectZero)];
    
}

#pragma mark -- 上啦刷新
- (void)hospitalTVheaderRereshing{
    
    [self.equipmentModelAry removeAllObjects];
    
    [self getEquipmentListInfoWithMiotLink];
    
}

#pragma mark -- 请求当前帐号设备

- (void)getEquipmentListInfoWithMiotLink{
    
    if([[LoginSingleton sharedLoginSingleton].cuid isEqualToString:@""]){
        [self.remoteListTableView.mj_header endRefreshing];
        return;
    }
    
    [self showIndicator:@"正在查询设备,请稍候..."];
    
    __weak __typeof(self) weakSelf = self;
    
    mmwGetPuListHTTPWebService * getPuListService = [[mmwGetPuListHTTPWebService alloc]initWithCuId:[LoginSingleton sharedLoginSingleton].cuid];
    
    getPuListService.host = MiotlinkWebServiceStr;
    [ getPuListService sendRequestWithSuccess:^(__kindof mmwHTTPBaseWebService *webService, id responseObject) {
        for (NSDictionary*dictionary in responseObject) {
            NSLog(@"success  %@ ----- %@",webService,responseObject);
            
            EquipmentModel *model = [[EquipmentModel alloc] init];
//            [model mj_setKeyValues:dictionary];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.equipmentModelAry addObject:model];
        }
        
        
        [weakSelf.remoteListTableView reloadData];
        [weakSelf.remoteListTableView.mj_header endRefreshing];
        //测试
//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"success" message:[NSString stringWithFormat:@"%@",responseObject] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
//        [alert show];
        [weakSelf hideIndicator];
    } failure:^(__kindof mmwHTTPBaseWebService *webService, NSError *error) {
        NSLog(@"fail %@ ------ %@",webService,error);
        [weakSelf hideIndicator];
        [weakSelf.remoteListTableView.mj_header endRefreshing];
        //测试
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"fail" message:error.localizedDescription delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
        [alert show];
    }];
    
    
}

#pragma mark -- TableViewDelegate & DataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CAPurifierDetailsVC *purifierDetailsVC = [[CAPurifierDetailsVC alloc] init];
    purifierDetailsVC.model = self.equipmentModelAry[indexPath.row];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:purifierDetailsVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.equipmentModelAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"RemoteCell";
    RemoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RemoteTableViewCell" owner:self options:nil] lastObject];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.equipmentModelAry[indexPath.row];
    
    return cell;
    
}

#pragma mark--scan delgate
//扫描后得到的字符串
-(void)GKHScanQCodeViewController:(GKHScanQCodeViewController *)lhScanQCodeViewController readerScanResult:(NSString *)result
                   responseObject:(id)responseObject{
    
    [ShowMessage showCenterViewMessage:result];
    
    
    
    CAAddingPurifierVC *adding = [[CAAddingPurifierVC alloc] init];
    adding.QrCode = result;
    adding.configurationInfo = responseObject;
    __weak __typeof(self) weakSelf = self;
    adding.addingBlock = ^{
        [weakSelf.equipmentModelAry removeAllObjects];
        [weakSelf.navigationController popViewControllerAnimated:YES];
        [weakSelf getEquipmentListInfoWithMiotLink];
    };
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:adding animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
    
}

- (void)scan{
    
    GKHScanQCodeViewController * sqVC = [[GKHScanQCodeViewController alloc]init];
    sqVC.delegate = self;
    
    UINavigationController * nVC = [[UINavigationController alloc]initWithRootViewController:sqVC];

    [self presentViewController:nVC animated:YES completion:^{
        
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)testNot:(id)sender {
    
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 设置触发通知的时间
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:4];
    NSLog(@"fireDate=%@",fireDate);
    
    notification.fireDate = fireDate;
    // 时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置重复的间隔
    notification.repeatInterval = kCFCalendarUnitEra;
    
    // 通知内容
    notification.alertBody =  @"这里的空气真不错！洗洗肺吧！";
    notification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知参数
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"顺义空气质量：优" forKey:@"key"];
    notification.userInfo = userDict;
    
    // 执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}




@end
